package com.coisshi.base.module.upload.controller;

import cn.dev33.satoken.util.SaResult;
import com.coisshi.base.common.exception.BizException;
import com.coisshi.base.module.upload.service.UploadService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 文件上传到本地接口
 */
@RestController
@RequestMapping("/api/upload")
@RequiredArgsConstructor
public class UploadController {

    private final UploadService uploadService;

    /**
     * 单文件上传接口
     *
     * @param file 文件
     * @return 上传成功后的URL
     */
    @PostMapping
    public SaResult upload(@RequestParam("file") MultipartFile file) throws BizException {
        String url = uploadService.upload(file);
        return SaResult.ok().setData(url);
    }

    /**
     * 多文件上传接口
     *
     * @param files 文件列表
     * @return 上传成功后的URL列表
     */
    @PostMapping("/batch")
    public SaResult uploadBatch(@RequestParam("files") List<MultipartFile> files) throws BizException {
        List<String> urls = uploadService.uploadBatch(files);
        return SaResult.ok().setData(urls);
    }
} 